$(document).ready(function(){
	$("input").val("");
	$('#showAgreementWnd').show();
	$('#showAgreementWnd').window({
		width:465,
		height: 500,
		modal:true,
		minimizable:false,
		maximizable:false,
		collapsible:false
	});

	$('#showAgreementWnd').window('close');  //关闭窗口
	
	$("#showAgreement").click(function(){
		$('#showAgreementWnd').window('open');
	});

	$("#close_win_btn").click(function(){
		$('#showAgreementWnd').window('close');
	});
	
	$('input[plugin="passwordStrength"]').passwordStrength({minLen:1,maxLen:200});
	
	$("#userCode").focus();
	$('#companyCode').combobox({
		editable: false,
		url: BASE_URL_USER_SEARCH +'/showOrganizations?level=1&v=' + Date.parse(new Date()),
		valueField: 'id',
		textField: 'orgName',
		method:'get',
		//data:"level=1",
		onSelect: function(record) {
			$("#companyCode").val(record.id);
			$('#orgId').combobox('clear');
			$('#orgId').combobox('reload',BASE_URL_USER_SEARCH +'/showOrganizations?parentId='+record.id+'&level=2&v=' + Date.parse(new Date()));
			//console.log(record);
		},
		loadFilter:function(data){
			return data;
		},
		onLoadSuccess: function() {
			var defaultId = $("#companyCode").combobox("getData")[0].id;
			$("#companyCode").combobox("select",defaultId);
		}
		
	});
	
	$('#orgId').combobox({
		editable: false,
		url: BASE_URL_USER_SEARCH +'/showOrganizations?level=2&v=' + Date.parse(new Date()),
		valueField: 'id',
		textField: 'orgName',
		method:'get',
		//data:"level=2",
		onSelect: function(record) {
			$("#orgId").val(record.id);
			//console.log(record);
		},
		loadFilter:function(data){
			return data;
		},
		onLoadSuccess: function() {
			var defaultId = $("#orgId").combobox("getData")[0].id;
			$("#orgId").combobox("select",defaultId);
		}
		
	});
	
	$(".easyui-validatebox").blur(function() {
		$(this).validatebox("validate");
	});

	$("#regist").click(function(e) {
		e.preventDefault();
	    if($("#regForm").form("validate")){
		   	if(!$("#agreement").is(":checked"))
		    {
		   		$.messager.alert('错误','<p>请同意注册协议喔!</p>','error');
		   		return;
		    }
			$("#regist").attr("disabled", "disabled").text("提交中...");
			RegistUser();
	    	/*$.ajax({
	    		type: "post",
	    		url: BASE_URL_USER_SEARCH +'/checkUserExists?v=' + Date.parse(new Date()),
	    		data: "userCode=" + $("#userCode").val(),
	    		dataType: "json",
	    		success: function(result) {
	    			if(result.isExist)
	    			{
	    				$("#regist").removeAttr("disabled").text("注册");
	    				$.messager.alert('错误','<p>账号已存在，请使用其他账号!</p>','error', function(){
	    					$("#userCode").focus();
	    				});
	    			}
	    			else
	    			{
	    				RegistUser();
	    			}
	    		},
    			error: function() {
    				$("#regist").removeAttr("disabled").text("注册");
    				$.messager.alert('错误','<p>服务异常，注册失败，请稍后重试!</p>', 'error');
    			}
	    	});*/
			//alert("good");
		}
	    else 
	    {
	    	$(".validatebox-invalid").eq(0).focus();
	    }
	});
	$("#cancel").click(function(e) {
		e.preventDefault();
		window.location.href = "/UserLogin.html";
		//window.location.reload();
	});	
});

function RegistUser() {
	var userInfoJSON = $("#regForm").serializeJson();
	userInfoJSON["password"] = RSAUtils.encryptedString(key,userInfoJSON["password"]);
	userInfoJSON["confirmPassword"] = "";
	delete userInfoJSON["confirmPassword"];
	//userInfoJSON["organization"] = BASE_URL_ORGANIZATION+'/'+ userInfoJSON["organization"];
	$.ajax({
		type: "post",
		url: BASE_URL_USER_CRUD +'/registeredUser',
		data: JSON.stringify(userInfoJSON),
		contentType: "application/json",
		success: function(result) {
			$("#regist").removeAttr("disabled").text("注册");
			if(result.backValue === true)
			{
				$.messager.alert('提示','<p>注册成功，请等待管理员授权</p>','info',function(){
					window.location.href = "UserLogin.html";
				});
			}
			else
			{
				$.messager.alert('错误','<p>注册失败!' + result.backValue + '</p>', 'error');
			}
		},
		error: function() {
			$("#regist").removeAttr("disabled").text("注册");
			$.messager.alert('错误','<p>服务异常，注册失败，请稍后重试!</p>', 'error');
		}
	});
}
